package com.ebt.m.customer.net.http;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.DeleteObjectRequest;
import com.alibaba.sdk.android.oss.model.DeleteObjectResult;
import com.alibaba.sdk.android.oss.model.GetObjectRequest;
import com.alibaba.sdk.android.oss.model.GetObjectResult;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.ebt.m.AppContext;
import com.ebt.m.customer.entity.Attachment;
import com.ebt.m.utils.OssUtil;
import com.ebt.m.utils.fileutils.FileUtils;

import java.io.File;
import java.util.List;

/**
 * OSS上传文件类
 * Created by archer.qi on 2016/12/17.
 */
public class OSSUploader {

    private static OssUtil oSSUtil;

    public static void init(Context context) {
        oSSUtil = OssUtil.getInstance(context);
        oSSUtil.init(context);
    }

    /**
     * 附件上传到AliYun
     *
     * @param bucketName
     * @param objectKey
     * @param filePath
     */
    public static void uploadEntity(String bucketName,
                                    String objectKey,
                                    String filePath,
                                    OSSCompletedCallback<PutObjectRequest, PutObjectResult> callback) {
        PutObjectRequest put = new PutObjectRequest(bucketName, objectKey, filePath);
        OssUtil.ossService.asyncPutObject(put, callback);
    }

    /**
     * 从AliYun删除附件
     *
     * @param bucketName
     * @param objectKey
     * @param callback
     */
    public static void deleteEntity(String bucketName,
                                    String objectKey,
                                    OSSCompletedCallback<DeleteObjectRequest, DeleteObjectResult> callback) {
        DeleteObjectRequest delete = new DeleteObjectRequest(bucketName, objectKey);
        OssUtil.ossService.asyncDeleteObject(delete, callback);
    }

    /**
     * 从AliYun下载附件
     *
     * @param bucketName
     * @param objectKey
     * @param completedCallback
     */
    public static OSSAsyncTask downloadEntity(String bucketName,
                                              String objectKey,
                                              OSSCompletedCallback<GetObjectRequest, GetObjectResult> completedCallback) {
        GetObjectRequest get = new GetObjectRequest(bucketName, objectKey);
        return OssUtil.ossService.asyncGetObject(get, completedCallback);
    }

    /**
     * 附件是否存在
     *
     * @param bucketName
     * @param objectKey
     * @return
     */
    public static boolean isEntityExists(String bucketName, String objectKey) {
        try {
            return OssUtil.ossService.doesObjectExist(bucketName, objectKey);
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 客户附件ObjectKey
     *
     * @param fileName
     * @return
     */
    public static String getCustomerAttachmentObjectKey(String fileName) {
        int agentId = AppContext.getCurrentUser().getUserId();
        return OssUtil.OBJECT_KEY_CUSTOMER
                + agentId
                + File.separator
                + OssUtil.KEY_ATTACHMENT
                + File.separator
                + fileName;
    }

    /**
     * 客户头像ObjectKey
     *
     * @param fileName
     * @return
     */
    public static String getCustomerPortraitObjectKey(String fileName) {
        int agentId = AppContext.getCurrentUser().getUserId();
        return OssUtil.OBJECT_KEY_CUSTOMER
                + agentId
                + File.separator
                + OssUtil.KEY_portrait
                + File.separator
//                + fileName;
                + System.currentTimeMillis();
    }

    /**
     * 客户录音ObjectKey
     *
     * @param fileName
     * @return
     */
    public static String getCustomerTapeObjectKey(String fileName) {
        int agentId = AppContext.getCurrentUser().getUserId();
        return OssUtil.OBJECT_KEY_CUSTOMER
                + agentId
                + File.separator
                + OssUtil.KEY_TAPE
                + File.separator
                + fileName;
    }

    /**
     * 上传客户模块附件
     *
     * @param filePath
     * @param callback
     */
    public static void uploadAttachment(
            String filePath,
            OSSCompletedCallback<PutObjectRequest, PutObjectResult> callback) {
        String objectKey = getCustomerAttachmentObjectKey(FileUtils.getAbsoluteFileName(filePath));
        uploadEntity(oSSUtil.bucketNameCustomer, objectKey, filePath, callback);
    }

    /**
     * 上传客户模块附件
     *
     * @param filePath
     * @param callback
     */
    public static void uploadPortrait(
            String filePath,
            OSSCompletedCallback<PutObjectRequest, PutObjectResult> callback) {
        String objectKey = getCustomerPortraitObjectKey(FileUtils.getAbsoluteFileName(filePath));
        uploadEntity(oSSUtil.bucketNameCustomer, objectKey, filePath, callback);
    }

    /**
     * 上传录音
     *
     * @param filePath
     * @param callback
     */
    public static void uploadTape(
            String filePath,
            OSSCompletedCallback<PutObjectRequest, PutObjectResult> callback) {
        String objectKey = getCustomerTapeObjectKey(FileUtils.getAbsoluteFileName(filePath));
        uploadEntity(oSSUtil.bucketNameCustomer, objectKey, filePath, callback);
    }

    /**
     * 删除客户模块附件
     *
     * @param fileName
     * @param callback
     */
    public static void deleteAttachment(String fileName,
                                        OSSCompletedCallback<DeleteObjectRequest, DeleteObjectResult> callback) {
        String objectKey = getCustomerAttachmentObjectKey(fileName);
        boolean exists = isEntityExists(oSSUtil.bucketNameCustomer, objectKey);
        if (exists) {
            deleteEntity(oSSUtil.bucketNameCustomer, objectKey, callback);
        }
    }

    /**
     * 下载客户模块附件
     *
     * @param fileName
     * @param completedCallback
     */
    public static OSSAsyncTask downloadAttachment(String fileName,
                                                  OSSCompletedCallback<GetObjectRequest, GetObjectResult> completedCallback) {
        String objectKey = getCustomerAttachmentObjectKey(fileName);
        return downloadEntity(oSSUtil.bucketNameCustomer, objectKey, completedCallback);
    }

    /**
     * 下载客户模块附件
     *
     * @param fileName
     * @param completedCallback
     */
    public static OSSAsyncTask downloadTape(String fileName,
                                            OSSCompletedCallback<GetObjectRequest, GetObjectResult> completedCallback) {
        String objectKey = getCustomerTapeObjectKey(fileName);
        return downloadEntity(oSSUtil.bucketNameCustomer, objectKey, completedCallback);
    }

    /**
     * 批量上传客户附件
     *
     * @param attachments
     * @param callback
     */
    public static void uploadMultiAttachments(
            List<Attachment> attachments,
            OSSCompletedCallback<PutObjectRequest, PutObjectResult> callback) {
        if (attachments == null || attachments.size() == 0) {
            return;
        }
        for (Attachment attachment : attachments) {
            if (attachment != null) {
                uploadAttachment(attachment.getPath(), callback);
            }
        }
    }

    /**
     * 批量删除客户附件
     *
     * @param fileNames
     * @param callback
     */
    public static void deleteMultiAttachments(List<String> fileNames,
                                              OSSCompletedCallback<DeleteObjectRequest, DeleteObjectResult> callback) {
        if (fileNames == null || fileNames.size() == 0) {
            return;
        }
        for (String fileName : fileNames) {
            if (!TextUtils.isEmpty(fileName)) {
                deleteAttachment(fileName, callback);
            }
        }
    }

    /**
     * 获取公共访问地址
     *
     * @param bucketName
     * @param objectKey
     * @return
     */
    public static String getPublicUrl(String bucketName, String objectKey) {
        return OssUtil.ossService.presignPublicObjectURL(bucketName, objectKey);
    }
}
